<?php
/*
	Aurus CMS - free OOP CMS
	Copyright (C) 2010  Nestor Yanchuk

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
if (! defined('ROOT')) die();

if (!isset($_SESSION['captcha_'.Url::getUrlPart(1)])) {
	die('Error: no captcha code generated');
}
class ParentCaptcha{
	var $width = 200;
	var $height = 60;
	var $fonts = array();
	var $font = array();

	function wave($img){
		$width=imagesx($img);
		$height=imagesy($img);
		$center = $width/2;
		$img2=imagecreatetruecolor($width, $height);
		$foreground_color = array(mt_rand(0,100), mt_rand(0,100), mt_rand(0,100));
		$background_color = array(mt_rand(200,255), mt_rand(200,255), mt_rand(200,255));
		$rand1 = mt_rand(700000, 1000000) / 15000000;
		$rand2 = mt_rand(700000, 1000000) / 15000000;
		$rand3 = mt_rand(700000, 1000000) / 15000000;
		$rand4 = mt_rand(700000, 1000000) / 15000000;
		$rand5 = mt_rand(0, 3141592) / 1000000;
		$rand6 = mt_rand(0, 3141592) / 1000000;
		$rand7 = mt_rand(0, 3141592) / 1000000;
		$rand8 = mt_rand(0, 3141592) / 1000000;
		$rand9 = mt_rand(400, 600) / 100;
		$rand10 = mt_rand(400, 600) / 100;
		for($x = 0; $x < $width; $x++){
			for($y = 0; $y < $height; $y++){

				$sx = $x + ( sin($x * $rand1 + $rand5) + sin($y * $rand3 + $rand6) ) * $rand9;
				$sy = $y + ( sin($x * $rand2 + $rand7) + sin($y * $rand4 + $rand8) ) * $rand10;

				if($sx < 0 || $sy < 0 || $sx >= $width - 1 || $sy >= $height - 1){
					$color = 255;
					$color_x = 255;
					$color_y = 255;
					$color_xy = 255;
				}else{
					$color = (imagecolorat($img, $sx, $sy) >> 16) & 0xFF;
					$color_x = (imagecolorat($img, $sx + 1, $sy) >> 16) & 0xFF;
					$color_y = (imagecolorat($img, $sx, $sy + 1) >> 16) & 0xFF;
					$color_xy = (imagecolorat($img, $sx + 1, $sy + 1) >> 16) & 0xFF;
				}

				if($color == $color_x && $color == $color_y && $color == $color_xy){
					$newcolor=$color;
				}else{
					$frsx = $sx - floor($sx);
					$frsy = $sy - floor($sy);
					$frsx1 = 1 - $frsx;
					$frsy1 = 1 - $frsy;
					$newcolor = floor( $color    * $frsx1 * $frsy1 +
					$color_x  * $frsx  * $frsy1 +
					$color_y  * $frsx1 * $frsy  +
					$color_xy * $frsx  * $frsy );
				}
				imagesetpixel($img2, $x, $y, imagecolorallocate($img2, $newcolor, $newcolor, $newcolor));
			}
		}
		$img3 = imagecreate($width,$height);
		$blue = imagecolorallocate($img3,255,255,255);
		imagecopy($img3,$img2,0,0,0,0,$width,$height);
		imagedestroy($img2);
		imagecolortransparent($img3,$blue);
		return $img3;
	}
	function get_fonts(){
		$dh = opendir(MAINDIR.'/modules/captcha/fonts/');
		while ($dha = readdir($dh)){
			if (strtolower(substr($dha,-4)) == '.ttf') {
				$fonts[] = $dha;
			}
		}
		closedir($dh);

		$this->font = MAINDIR.'/modules/captcha/fonts/'.$fonts[mt_rand(0,(count($fonts)-1))];
	}
	function out_img($i){
		Image::outImage($i);
	}
}
include(MAINDIR.'/modules/captcha/captcha/'.mt_rand(1,4).'.php');

new Captcha( $_SESSION['captcha_'. Url::getUrlPart(1)] );
exit();